package Section;

import Designation.*;
import java.sql.*;
import javax.swing.JOptionPane;
import connectDB.ConnectDB;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Sec_Insert extends javax.swing.JFrame {

    ConnectDB connect = new ConnectDB();

    public Sec_Insert() {

        connect.getConnect();
        initComponents();
        cbbDepNoInsert.removeAllItems();
        setCbb();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtSectIdInsert = new javax.swing.JTextField();
        txtSectNameInsert = new javax.swing.JTextField();
        btnSectInsert = new javax.swing.JButton();
        btnSectReset = new javax.swing.JButton();
        btnSectCancel = new javax.swing.JButton();
        txtSectInchInsert = new javax.swing.JTextField();
        cbbDepNoInsert = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnSectInsert.setText("Insert");
        btnSectInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSectInsertActionPerformed(evt);
            }
        });

        btnSectReset.setText("Reset");
        btnSectReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSectResetActionPerformed(evt);
            }
        });

        btnSectCancel.setText("Cancel");
        btnSectCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSectCancelActionPerformed(evt);
            }
        });

        cbbDepNoInsert.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("SECTID");

        jLabel2.setText("SECTNAME");

        jLabel3.setText("SECTINCH");

        jLabel4.setText("DEPTNO");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(btnSectInsert)
                .addGap(58, 58, 58)
                .addComponent(btnSectReset, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                .addComponent(btnSectCancel)
                .addGap(42, 42, 42))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbbDepNoInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(txtSectInchInsert, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtSectIdInsert, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtSectNameInsert, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(103, 103, 103))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSectIdInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSectNameInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSectInchInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbbDepNoInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSectInsert)
                    .addComponent(btnSectReset)
                    .addComponent(btnSectCancel))
                .addContainerGap(66, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSectInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSectInsertActionPerformed
        // TODO add your handling code here:
        checkinsertSec();


    }//GEN-LAST:event_btnSectInsertActionPerformed

    private void btnSectCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSectCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnSectCancelActionPerformed

    private void btnSectResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSectResetActionPerformed
        // TODO add your handling code here:
        resetSec();
    }//GEN-LAST:event_btnSectResetActionPerformed

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Sec_Insert().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSectCancel;
    private javax.swing.JButton btnSectInsert;
    private javax.swing.JButton btnSectReset;
    private javax.swing.JComboBox cbbDepNoInsert;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtSectIdInsert;
    private javax.swing.JTextField txtSectInchInsert;
    private javax.swing.JTextField txtSectNameInsert;
    // End of variables declaration//GEN-END:variables

    private void insertSec() {
        String id = txtSectIdInsert.getText();
        String name = txtSectNameInsert.getText();
        String inch = txtSectInchInsert.getText();
        int deptno = Integer.parseInt(cbbDepNoInsert.getSelectedItem().toString());
        try {
            String query = "INSERT INTO Sections VALUES (?,?,?,?)";

            PreparedStatement pst = connect.cn.prepareStatement(query);
            pst.setString(1, id);
            pst.setString(2, name);
            pst.setString(3, inch);
            pst.setInt(4, deptno);



            pst.executeUpdate();

            JOptionPane.showMessageDialog(this, "Insert sucessful");



        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "Insert fail");
        }
    }

    private void resetSec() {
        txtSectIdInsert.setText("");
        txtSectNameInsert.setText("");
        txtSectInchInsert.setText("");
        cbbDepNoInsert.setSelectedIndex(0);
    }

    private void checkinsertSec() {
        if (txtSectIdInsert.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "ID cannot blank");
            return;
        }

        try {
            String query5 = "Select SECTID from Sections";
            Statement st = connect.cn.createStatement();
            ResultSet rs = st.executeQuery(query5);
            while (rs.next()) {
                if (txtSectIdInsert.getText().equals(rs.getString("SECTID"))) {
                    JOptionPane.showMessageDialog(this, "ID be duplicated");
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        if (txtSectIdInsert.getText().length() > 10) {
            JOptionPane.showMessageDialog(this, "ID cannot be longer than 10 characters");
            return;
        }

        if (txtSectNameInsert.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Name cannot blank");
            return;
        }
        
        if (txtSectNameInsert.getText().length() > 15) {
            JOptionPane.showMessageDialog(this, "Name cannot be longer than 15 characters");
            return;
        }
        
        if (txtSectInchInsert.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "SecInch cannot blank");
            return;
        }
        
        if (txtSectInchInsert.getText().length() > 10) {
            JOptionPane.showMessageDialog(this, "SecInch cannot be longer than 10 characters");
            return;
        }

        insertSec();

    }

    private void setCbb() {
        cbbDepNoInsert.removeAllItems();
        try {

            String query2 = "Select DEPTNO from Sections";
            Statement st = connect.cn.createStatement();
            ResultSet rs = st.executeQuery(query2);
            while (rs.next()) {
                cbbDepNoInsert.addItem(rs.getString("DEPTNO"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(Sec_Insert.class.getName()).log(Level.SEVERE, null, ex);

        }

    }
}
